package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.constant.ResultCode;
import com.yiyang.domain.ResultPage;
import com.yiyang.domain.dto.CustomerDto;
import com.yiyang.domain.po.Bed;
import com.yiyang.domain.po.BedDetails;
import com.yiyang.domain.po.Customer;
import com.yiyang.domain.vo.CustomerVo;
import com.yiyang.exception.BusinessException;
import com.yiyang.mapper.BedDetailsMapper;
import com.yiyang.mapper.BedMapper;
import com.yiyang.service.CustomerService;
import com.yiyang.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author itcast
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2024-12-25 11:08:00
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResultPage<CustomerVo> listKhxxPage(CustomerDto customerDto) {

        // 分页条件
        Page<CustomerVo> customerPage = new Page<>(customerDto.getPage(),customerDto.getPageSize());

        customerMapper.selectPageByCondition(customerPage,customerDto);

        return new ResultPage<CustomerVo>(customerPage.getTotal(),customerPage.getRecords());
    }

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private BedDetailsMapper bedDetailsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rzdj(Customer customer) {
        // 确认床位是否被占用
        Bed bed = bedMapper.selectById(customer.getBedId());
        if(bed.getStatus() == 2){
            throw new BusinessException(ResultCode.BED_EXCHANGE_CODE,"当前床位已被占用,请重新选择");
        }
        // 修改床位状态为已占用状态
        bed.setStatus(2);
        bedMapper.updateById(bed);
        // 新增客户信息到客户表
        customer.setBuildingNo("606");
        customerMapper.insert(customer);
        // 新增床位详情信息
        BedDetails bedDetails = new BedDetails();
        bedDetails.setBedDetails("606#"+bed.getBedNo());
        bedDetails.setStartDate(customer.getCheckinDate());
        bedDetails.setEndDate(customer.getExpirationDate());
        bedDetails.setCustomerId(customer.getId());
        bedDetails.setBedId(bed.getId());

        bedDetailsMapper.insert(bedDetails);
    }

    @Override
    public void editKhxx(Customer customer) {
        //查询旧的客户信息
        Customer oldCustomer = customerMapper.selectById(customer.getId());
        // 比较合同到期时间
        if(!oldCustomer.getExpirationDate().equals(customer.getExpirationDate())){
            LambdaUpdateWrapper<BedDetails> lqw = new LambdaUpdateWrapper<>();
            lqw.eq(BedDetails::getCustomerId,customer.getId());
            lqw.eq(BedDetails::getIsDeleted,0);
            BedDetails bedDetails = bedDetailsMapper.selectOne(lqw);

            bedDetails.setEndDate(customer.getExpirationDate());
            bedDetailsMapper.updateById(bedDetails);
        }

        // 修改客户信息
        customerMapper.updateById(customer);
    }

    @Override
    public void removeCustomer(Integer id, Integer bedId) {
        // 客户信息逻辑删除
        Customer customer = new Customer();
        customer.setId(id);
        customer.setIsDeleted(1);

        customerMapper.updateById(customer);

        // 修改床位状态为空闲
        Bed bed = new Bed();
        bed.setId(bedId);
        bed.setStatus(1);
        bedMapper.updateById(bed);

        // 逻辑删除床位详情
        LambdaUpdateWrapper<BedDetails> luw = new LambdaUpdateWrapper<>();
        luw.set(BedDetails::getIsDeleted,1);
        luw.eq(BedDetails::getCustomerId,id);
        bedDetailsMapper.update(null,luw);

    }
}




